<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将字符串转换为数组</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.str-shuffle.html">str_shuffle</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.str-word-count.html">str_word_count</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.str-split" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">str_split</h1>
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">str_split</span> &mdash; <span class="dc-title">将字符串转换为数组</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.str-split-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>str_split</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$split_length</code><span class="initializer"> = 1</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   将一个字符串转换为数组。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.str-split-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">string</code></em></dt>

     <dd>

      <p class="para">
       输入字符串。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">split_length</code></em></dt>

     <dd>

      <p class="para">
       每一段的长度。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.str-split-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果指定了可选的 <em><code class="parameter">split_length</code></em> 参数，返回数组中的每个元素均为一个长度为 <em><code class="parameter">split_length</code></em> 的字符块，否则每个字符块为单个字符。
  </p>
  <p class="para">
   如果 <em><code class="parameter">split_length</code></em> 小于 1，返回 <strong><code>FALSE</code></strong>。如果 <em><code class="parameter">split_length</code></em> 参数超过了 <em><code class="parameter">string</code></em> 超过了字符串 <em><code class="parameter">string</code></em> 的长度，整个字符串将作为数组仅有的一个元素返回。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.str-split-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4784">
    <p><strong>Example #1 <span class="function"><strong>str_split()</strong></span> 使用范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;Friend"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$arr1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_split</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$arr2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_split</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; H
    [1] =&gt; e
    [2] =&gt; l
    [3] =&gt; l
    [4] =&gt; o
    [5] =&gt;
    [6] =&gt; F
    [7] =&gt; r
    [8] =&gt; i
    [9] =&gt; e
    [10] =&gt; n
    [11] =&gt; d
)

Array
(
    [0] =&gt; Hel
    [1] =&gt; lo
    [2] =&gt; Fri
    [3] =&gt; end
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.str-split-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.chunk-split.html" class="function" rel="rdfs-seeAlso">chunk_split()</a> - 将字符串分割成小块</span></li>
    <li class="member"><span class="function"><a href="function.preg-split.html" class="function" rel="rdfs-seeAlso">preg_split()</a> - 通过一个正则表达式分隔字符串</span></li>
    <li class="member"><span class="function"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - 使用一个字符串分割另一个字符串</span></li>
    <li class="member"><span class="function"><a href="function.count-chars.html" class="function" rel="rdfs-seeAlso">count_chars()</a> - 返回字符串所用字符的信息</span></li>
    <li class="member"><span class="function"><a href="function.str-word-count.html" class="function" rel="rdfs-seeAlso">str_word_count()</a> - 返回字符串中单词的使用情况</span></li>
    <li class="member"><a href="control-structures.for.html" class="link">for</a></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.str-shuffle.html">str_shuffle</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.str-word-count.html">str_word_count</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
